package com.hfzy.ihk.web.calldata.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.facade.calldata.entity.CallHistory;
import com.hfzy.ihk.facade.calldata.entity.CallHistoryText;
import com.hfzy.ihk.facade.calldata.service.EsOptRmiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/11.
 */
@RestController
@RequestMapping("/callTaskHistory")
public class CallTaskHistoryController {

    @Autowired
    EsOptRmiService esOptRmiService;

    @RequestMapping("/updateCallHistory")
    public boolean updateCallHistory(String jsonStr){
        String result = esOptRmiService.createOrUpdateCloudCallHistoryBySessionId(jsonStr);
        return result.equals("1");
    }

    @RequestMapping("/saveCallHistoryText")
    public boolean saveCallHistoryText(String jsonStr){
        String result = esOptRmiService.createCallHistoryTextById(jsonStr);
        return result.equals("1");
    }



    @RequestMapping("/queryCallHistoryTextList")
    public Map<String,Object> queryCallHistoryTextList(@RequestParam Map<String,Object> paramMap, HttpServletRequest request){



        Map<String, Object> result = esOptRmiService.queryCallHistoryTextList(paramMap);

        return result;
    }

    @RequestMapping("/queryCallHistoryList")
    public Map<String,Object> queryCallHistoryList(@RequestParam Map<String,Object> paramMap){

        Map<String, Object> result = esOptRmiService.queryCallHistoryList(paramMap);

        return result;
    }



    @RequestMapping("/searchCallHistoryTextList")
    public Map<String,Object> searchCallHistoryTextList(@RequestParam Map<String,Object> paramMap){

        Map<String,Object> result=new HashMap<>();

        List<CallHistoryText> callHistoryText =esOptRmiService.searchCallHistoryTextList(paramMap);

        result.put("data", JSON.toJSONString(callHistoryText));

        return result;
    }


    @RequestMapping("/callTaskCompletion")
    public Map<String,Object> callTaskCompletion(@RequestParam Map<String,Object> paramMap){

        Map<String,Object> resultMap=new HashMap<>();

        String result = esOptRmiService.callTaskCompletion((String)paramMap.get("callTaskId"));

        resultMap.put("result",result);

        return resultMap;
    }


    @RequestMapping("/createOrUpdateCloudCallHistoryBySessionId")
    public Map<String,Object> createOrUpdateCloudCallHistoryBySessionId(@RequestParam Map<String,Object> paramMap){

        Map<String,Object> resultMap=new HashMap<>();

        String result = esOptRmiService.createOrUpdateCloudCallHistoryBySessionId((String)paramMap.get("saveData"));

        resultMap.put("result",result);

        return resultMap;
    }


    @RequestMapping("/queryCallTaskDetailId")
    public Map<String,Object> queryCallTaskDetailId(@RequestParam Map<String,Object> paramMap){

        Map<String, Object> resultMap = esOptRmiService.queryCallTaskDetailId((String) paramMap.get("callTaskId"));


        return resultMap;
    }


    /**
     * fs保存呼叫记录
     * @param jsonStr
     * @return
     */
    @RequestMapping("/fsCreateCallHistory")
    @CrossOrigin(origins = "*")
    public boolean fsSaveCallHistoryText(String jsonStr){
        String result = esOptRmiService.createOrUpdateCloudCallHistoryBySessionId(jsonStr);
        return result.equals("1");
    }


    /**
     *fs更新通话记录,这是当通话结束的时候,由freeswitch发起的请求
     * @param object
     * @return
     */
    @RequestMapping("/fsUpdateCallHistory")
    @CrossOrigin(origins = "*")
    public boolean fsSaveCallHistoryText(@RequestBody JSONObject object){

        CallHistory callHistory=new CallHistory();

        System.out.println(object);

        return true;
    }
}
